<template>
  <view>
    <!-- 选择收获地址盒子 -->
    <view class="adress-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择收获地址+</button>
    </view>
    <!-- 收获地址信息盒子 -->
    <view class="adress-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="row1-left">收货人： <text>{{address.userName}}</text></view>
        <view class="row1-right">
          <view class="phone">电话： <text>{{address.telNumber}}</text></view>
          <uni-icons type="arrowright" size="17"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="row2-left">收获地址:&nbsp;</view>
        <view class="row2-right">{{addstr}}</view>
      </view>
    </view>
    <image src="/static/cart_border@2x.png" class="adress-boder"></image>

  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    name: "my-adress",
    data() {
      return {
        // address: {}
      };
    },
    methods: {
      ...mapMutations('m_user', ['UPDATE_ADDRESS']),
      // 选择收获地址
      async chooseAddress() {
        //    调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
        //    返回值是一个数组：第 1 项为错误对象；第 2 项为成功之后的收货地址对象
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        // 判断
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          // this.address =  succ
          // 使用vuex存储
          this.UPDATE_ADDRESS(succ)
        }
        // 解决取消授权之后无法再次授权
        if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
          this.reAuth()
        }
      },
      async reAuth() {
        // 弹出框 询问是否打开权限
        const [err2, confirmResults] = await uni.showModal({
          content: '检测到您没打开地址权限，是否去设置打开？',
          confirmText: '确定',
          cancelText: '取消'
        })
        if (err2) return
        // console.log(confirmResults.confirm)
        // 如果点击了取消 则提示用户取消授权
        if (confirmResults.cancel) return uni.$showMsg('您已取消了地址授权！')
        // 点击确定授权 则 uni.openSetting 调用这个方法进行授权
        if (confirmResults.confirm) return uni.openSetting({
          //  授权结束，需要对授权的结果做进一步判断
          success: (settingResult) => {
            settingResult.authSetting = {
              "scope.address": true,
            }
            // 地址授权的值等于 true，提示用户 “授权成功”
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            // 地址授权的值等于 false，提示用户 “您取消了地址授权”
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
      }
    },
    computed: {
      ...mapState('m_user', ['address']),
      // 收货的详细地址
      ...mapGetters('m_user', ['addstr'])
      // addstr() {
      //   if (!this.address.provinceName) return ''
      //   return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
      // }
    }
  }
</script>

<style lang="scss">
  .adress-boder {
    display: block;
    width: 100%;
    height: 5px;
  }

  .adress-choose-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .adress-info-box {
    font-size: 13px;
    display: flex;
    height: 90px;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;

    .row1 {
      display: flex;
      justify-content: space-between;

      .row1-right {
        display: flex;
        align-items: center;

        .phone {
          margin-right: 6px;
        }
      }
    }

    .row2 {
      display: flex;
      align-items: center;
      margin-top: 10px;

      .row2-left {
        white-space: nowrap;
        margin-right: 6px;
      }
    }
  }
</style>
